package com.judian.smart.db;

import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.judian.smart.base.MyApplication;
import com.judian.smart.util.ProjectUtils;

import java.io.File;

/**
 * author: Peter
 * date: 2019/08/09 星期五 11:01
 * desc: 数据库操作
 */
public class Database {
    private static final String DB_NAME = "homedatabases.db";
    private SQLiteDatabase db;

    public Database() {
        open();
    }

    /**
     * 关闭数据库
     */
    public void close() {
        if (db != null) {
            db.close();
            db = null;
        }
    }

    /**
     * 打开
     */
    private void open() throws SQLException {
        try {
            String databaseFilename = ProjectUtils.getRootPath().getRootPath();
            if (new File(databaseFilename).exists()) {
                db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
            } else {
                databaseFilename = MyApplication.getInstance().getRootPath()
                        + File.separator + "Root" + File.separator + DB_NAME;
                db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
            }
        } catch (Exception e) {
            System.out.println("Database_Exception:" + e);
        }
    }

    public SQLiteDatabase getSQLiteDatabase() {
        return this.db;
    }
}
